 package com.suozq.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 锁战强
 * @date 2022/04/02
 */
public class FileChannelSimpleUse {
    
    public static void main(String[] args){
        
        try(RandomAccessFile rFile = new RandomAccessFile("G:/test.txt", "rw");FileChannel fileChannel = rFile.getChannel()){
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            StringBuilder sb = new StringBuilder();
            while(fileChannel.read(buffer)>0) {
                buffer.flip();
                byte[] bytes = new byte[buffer.limit()];
                buffer.get(bytes);
                sb.append(new String(bytes));
                buffer.clear();
            }
            System.out.println(sb.toString());
        }catch (IOException e) {
            e.printStackTrace();
        }
        
    }
}   
